package com.danny.componenttest

import android.app.Application
import com.alibaba.android.arouter.launcher.ARouter
import com.danny.base.AppConfig
import com.danny.base.BaseApp

class MainApplication: BaseApp() {

    override fun onCreate() {
        super.onCreate()
        if (isDebug()){
            // 这两行必须写在init之前，否则这些配置在init过程中将无效
            ARouter.openLog()
            // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
            ARouter.openDebug()
        }
        // 初始化 ARouter
        ARouter.init(this)
        initModuleApp(this)
        initModuleData(this)
    }

    private fun isDebug(): Boolean{
        return BuildConfig.DEBUG
    }

    override fun initModuleApp(application: Application) {
        for (moduleApp in AppConfig.moduleApps){
            try {
                val clazz = Class.forName(moduleApp)
                val baseApp = clazz.newInstance() as BaseApp
                baseApp.initModuleApp(this)
            } catch (e: ClassNotFoundException) {
                e.printStackTrace()
            } catch (e: IllegalAccessError) {
                e.printStackTrace()
            } catch (e: InstantiationError) {
                e.printStackTrace()
            }
        }
    }

    override fun initModuleData(application: Application) {
        for (moduleApp in AppConfig.moduleApps) {
            try {
                val clazz = Class.forName(moduleApp)
                val baseApp = clazz.newInstance() as BaseApp
                baseApp.initModuleData(this)
            } catch (e: Exception){
                e.printStackTrace()
            }
        }
    }
}